﻿using System;
using System.Collections.Generic;
using System.Text;

namespace pgen
{
    public class PinCode
    {
        public int Part1 { get; set; }

        public int Part2 { get; set; }

        public int Part3 { get; set; }

        public int Part4 { get; set; }

        public int PinLength { get; set; }

        private readonly Random _rand;

        public PinCode(int length)
        {
            PinLength = length;
            Reset();
            _rand = new Random(Environment.TickCount);
        }

        public void Reset()
        {
            Part1 = Part2 = Part3 = Part4 = 0;
        }

        private int GetRandomInt()
        {
            return _rand.Next(1000, 9999);
        }

        public void Seed()
        {
            Reset();
            Part1 = GetRandomInt();
            Part2 = GetRandomInt();
            if (PinLength >  8) Part3 = GetRandomInt();
            if (PinLength > 12) Part4 = GetRandomInt();
        }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(String.Format("{0:0000}", Part1));
            sb.Append(String.Format("{0:0000}", Part2));
            if (PinLength >  8) sb.Append(String.Format("{0:0000}", Part3));
            if (PinLength > 12) sb.Append(String.Format("{0:0000}", Part4));
            return sb.ToString();
        }

        public long ToLong()
        {
            return long.Parse(ToLongString());
        }

        public string ToLongString()
        {
            var sb = new StringBuilder();
            sb.Append(Part1.ToString());
            sb.Append(Part2.ToString());
            if (PinLength > 8) sb.Append(Part3.ToString());
            if (PinLength > 12) sb.Append(Part4.ToString());
            return sb.ToString();
        }
    }
}
